<?php

namespace addons\booking\model;

use think\Model;


class Goods extends Model
{

    // 表名
    protected $name = 'booking_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'image_text'
    ];


    protected static function init()
    {
    }

    public function getStatusList()
    {
        return ['up' => __('Status up'), 'down' => __('Status down')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getImageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');
        return cdnurl($value, true);
    }

    public function getImagesAttr($value)
    {
        if (!$value) {
            return [];
        }
        $arr = explode(',', $value);
        foreach ($arr as &$image) {
            $image = cdnurl($image, true);
        }
        return $arr;
    }


    public static function tableList($param)
    {
        $pageNum = 10;
        if (isset($param['num']) && !empty($param['num'])) {
            $pageNum = $param['num'];
        }

        $list = self::field('id,name,image,price,sales,views,intro,market_price,createtime')
            ->where(function ($query) use ($param) {

                $query->where('status', 'up');

                if (isset($param['keyword']) && !empty($param['keyword'])) {
                    $query->where('name', 'like', '%' . $param['keyword'] . '%');
                }
                if (isset($param['store_id']) && !empty($param['store_id'])) {
                    $query->where('store_id',$param['store_id']);
                }
                
                if (isset($param['category_id']) && !empty($param['category_id'])) {
                    $param['category_id'] = (int)$param['category_id'];
                    $query->where(function ($query) use ($param) {
                        $category = \addons\booking\model\GoodsCategory::where('pid', $param['category_id'])->select();
                        $sqlArr = ["FIND_IN_SET('" . (int)$param['category_id'] . "', `category_ids`)"];
                        foreach ($category as $item) {
                            $item['id'] = (int)$item['id'];
                            $sqlArr[] = "FIND_IN_SET('" . (int)$item['id'] . "', `category_ids`)";
                        }
                        $query->whereOr(implode(' OR ', $sqlArr));
                    });
                }
            })->order('weigh desc,id desc')->paginate($pageNum);

        return $list;
    }

    //详情
    public static function detail($id)
    {
        $info = self::with([
            'Comment' => function ($query) {
                $query->with([
                    'replay' => [
                        'Manage' => function ($query) {
                            $query->field('id,nickname');
                        }
                    ],
                    'User'   => function ($u) {
                        $u->field('id,nickname,avatar');
                    }
                ])->field('id,source_id,user_id,comment,grade')->where('type', 1)->where('status', 'normal')->limit(10);
            }
        ])->field('id,name,image,price,sales,images,views,content,intro,market_price,createtime')
            ->where('id', $id)
            ->where('status', 'up')
            ->find();
        if ($info) {
            $info->setInc('views');
            $comment = collection($info->comment)->toArray();
            foreach ($comment as &$item) {
                if ($item['user']) {
                    $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
                }
            }
            $info->setRelation('comment', $comment);
        }
        return $info;
    }

    public function Comment()
    {
        return $this->hasMany('Comment', 'source_id', 'id');
    }
}
